package com.central.apps.controller;

import com.central.app.service.HandlerService;
import org.apache.commons.collections.MapUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/flow")
public class FlowHistoryController {
    @Autowired private HandlerService handlerService;

    @PostMapping("/exportFlowHistoryExcel")
    public Object ExportFlowHistoryExcel(@RequestBody Map<String,Object> params, HttpServletResponse response) throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet对象（excel的表单）
        HSSFSheet sheet=wb.createSheet("sheet1");
        HSSFRow row=sheet.createRow(0);
        //在这一行创建单元格
        //列的最小值标识也是0
        row.createCell(0).setCellValue("流程节点名称");
        row.createCell(1).setCellValue("审批人名称");
        row.createCell(2).setCellValue("流程审批日期");
        row.createCell(3).setCellValue("流程审批状态");
        row.createCell(4).setCellValue("流程审批意见");
        //行标识:下次创建行直接从第二行开始
        int rownum =1;
        //获取数据
        String appName = MapUtils.getString(params,"appName");
        String bizid =MapUtils.getString(params,"bizid");
        List<Map<String, Object>> bySql = handlerService.findBySql("select nodename,nickname,opiniontime,IF(flag=1,'同意','拒绝')as flag,opinion from biz_flow_opinion where app='" + appName + "' and bizid = " + bizid);
        for(Map<String,Object> map :bySql){
            HSSFRow row1 = sheet.createRow(rownum);
            row1.createCell(0).setCellValue(MapUtils.getString(map,"nodename"));
            row1.createCell(1).setCellValue(MapUtils.getString(map,"nickname"));
            row1.createCell(2).setCellValue(MapUtils.getString(map,"opiniontime"));
            row1.createCell(3).setCellValue(MapUtils.getString(map,"flag"));
            String opinion = MapUtils.getString(map, "opinion");
            String s = opinion.replaceAll("<p>", "");
            String s1 = s.replaceAll("</p>", "");
            row1.createCell(4).setCellValue(s1);
            rownum++;
        }
        //输出Excel文件
        OutputStream output=response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=FlowHistoryExcel.xls");
        response.setContentType("application/x-xls");
        wb.write(output);
        output.close();
        return null;
    }
}
